<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<input type="hidden" id="differenceQtyValue" name="differenceQtyValue">
<input type="hidden" id="differenceWeightValue" name="differenceWeightValue">
<input type="hidden" id="adjustQtyValue" name="adjustQtyValue">
<input type="hidden" id="adjustWeightValue" name="adjustWeightValue">

<!--盘亏保存模态框-->
<div class="modal fade" id="modal-lossCreate">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <form id="adjustLossForm" class="form-horizontal" role="form">
                    <input type="hidden" id="ajaItyId" name="ajaItyId">
                    <input type="hidden" id="ajaCdlId" name="ajaCdlId">
                    <input type="hidden" id="ajaId" name="ajaId">
                    <input type="hidden" id="ajaCpnId" name="ajaCpnId">

                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">调整数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="adjustQty" name="ajaNum"
                                   autocomplete="off" placeholder="请输入调整数量">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">调整重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="adjustWeight" name="ajaWeight"
                                   autocomplete="off" placeholder="请输入调整重量">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-lossCommit">确定</button>
            </div>
        </div>
    </div>
</div>
<!--盘盈保存模态框-->
<div class="modal fade" id="modal-profitCreate">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <form id="adjustProfitForm" class="form-horizontal" role="form">
                    <input type="hidden" id="profitCdlId" name="ajaCdlId">
                    <input type="hidden" id="profitAjaId" name="ajaId">
                    <input type="hidden" id="profitCpnId" name="ajaCpnId">

                    <div class="form-group">
                        <label class="col-sm-2 control-label no-padding-right">调整数量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="adjustProfitQty" name="ajaNum"
                                   autocomplete="off" placeholder="请输入调整数量">
                        </div>
                        <label class="col-sm-2 control-label no-padding-right">调整重量</label>
                        <div class="col-sm-4">
                            <input type="number" step="0.1" style="padding-right: 5px;"
                                   class="form-control" id="adjustProfitWeight" name="ajaWeight"
                                   autocomplete="off" placeholder="请输入调整重量">
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-info" id="btn-profitCommit">确定</button>
            </div>
        </div>
    </div>
</div>
<section class="content-header">
    <h1>
        损溢明细调整&nbsp;&nbsp;&nbsp;&nbsp;<span style="font-size: 19px;">计划编号：</span><span style="font-size: 19px;"
                                                                                       th:text="${checkPlan.cpnCode}"></span>
    </h1>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a th:if="${checkPlan.cpnAdjStatus==3}"
           th:href="@{'/material/checkPlan/resultCommit/'+${checkPlan.cpnId}}"
           shiro:hasPermission="matinv:matchecksub:view" id="btn-next"
           class="btn btn-warning">下一步
        </a>
    </div>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a href="/material/checkPlan/plan" shiro:hasPermission="matinv:matcheckplan:view"
           id="btn-return-checkPlanList"
           class="btn btn-info">返回计划列表
        </a>
    </div>
    <div class="pull-right"
         style="margin:0px 5px 5px 5px;text-align: right">
        <a th:href="@{'/material/checkPlan/resultSave/'+${checkPlan.cpnId}}"
           shiro:hasPermission="matinv:matcheckresult:view" id="btn-prev"
           class="btn btn-success">上一步
        </a>
    </div>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <div class="row">
                        <div class="col-md-7">
                            <div class="row">
                                <div class="col-md-9">
                                    <h4>盘点损溢明细</h4>
                                </div>
                                <div class="col-md-3">
                                    <button shiro:hasPermission="matinv:matcheckadj:edit" id="btn-updateAdjStatus"
                                            class="btn btn-success pull-right"
                                            th:text="@{(${checkPlan.cpnAdjStatus} == 3 ? '撤销' : '提交')}"
                                            th:style="'display:' + @{(${checkPlan.cpnPalStatus} == 3 ? 'none;' : 'inline-block;')}">
                                    </button>
                                </div>
                            </div>
                            <table id="bootstrap-table"></table>
                        </div>
                        <div class="col-md-5">
                            <input type="hidden" id="cpnId" name="cpnId"
                                   th:value="${checkPlan.cpnId}">
                            <input type="hidden" id="cdlId" name="cdlId">
                            <input type="hidden" id="cdlMatId" name="cdlMatId">
                            <input type="hidden" id="matSpecName">
                            <input type="hidden" id="matLength">
                            <input type="hidden" id="matWidth">
                            <input type="hidden" id="matHeight">
                            <input type="hidden" id="matFormulaType">
                            <div class="row">
                                <div class="col-md-12">
                                    <h4>损溢明细调整</h4>
                                </div>
                            </div>
                            <div style="position:relative;">
                                <table id="bootstrap-stock-table"></table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">

    /** 加载表格 */
    var table1, table2;

    /** 损溢明细调整 */
    function adjustStockList() {
        table2 = js.table.init({
            id: "bootstrap-stock-table",
            url: ctx + "material/checkPlan/adjustStockList",
            pageSize: 300,
            showExport: false,
            queryData: {
                ajaCdlId: $("#cdlId").val(),
                ajaMatId: $("#cdlMatId").val()
            },
            columns: [
                {title: '项目名称', field: 'projectName'},
                {title: '在库数量', field: 'ajaItyNum'},
                {title: '在库重量', field: 'ajaItyWeight'},
                {title: '调整数量', field: 'ajaNum'},
                {title: '调整重量', field: 'ajaWeight'},
                {
                    title: '操作', field: 'ajaItyId',
                    formatter: function (value, row, index) {
                        var actions = [];
                        if (hasP("matinv:matcheckadj:edit") && row.profitLossFlg == 1 && row.ajaItyStatus != 3) {
                            // 盘盈
                            actions.push("<a class='btn btn-warning btn-info btn-xs'  href='javascript:void(0);' onclick='profitUpdate(" + value + "," + JSON.stringify(row) + ")'>调整</a>");
                        } else if (hasP("matinv:matcheckadj:edit") && row.profitLossFlg == 2 && row.ajaItyStatus != 3) {
                            // 盘亏
                            actions.push("<a class='btn btn-warning btn-info btn-xs'  href='javascript:void(0);' onclick='lossUpdate(" + value + "," + JSON.stringify(row) + ")'>调整</a>");
                        }
                        return actions.join('');
                    }
                }
            ]
        });
    }

    /** 盘点损溢明细行点击 */
    function table1ClickRow(data) {

        $("#cdlMatId").val(data.cdlMatId);
        $("#cdlId").val(data.cdlId);

        $("#matSpecName").val(data.matSpecName);
        $("#matWidth").val(data.matWidth);
        $("#matLength").val(data.matLength);
        $("#matHeight").val(data.matHeight);
        $("#matFormulaType").val(data.matFormulaType);

        // 损益数量 损益重量 调整数量 调整重量
        var differenceQtyValue = Math.abs(data.differenceNum) == "" ? 0 : Math.abs(data.differenceNum);
        var differenceWeightValue = Math.abs(data.differenceWeight) == "" ? 0 : Math.abs(data.differenceWeight);
        var adjustQty = Math.abs(data.adjustNum) == "" ? 0 : Math.abs(data.adjustNum);
        var adjustWeight = Math.abs(data.adjustWeight) == "" ? 0 : Math.abs(data.adjustWeight);
        $("#differenceQtyValue").val(Math.round(differenceQtyValue * 100) / 100);
        $("#differenceWeightValue").val(Math.round(differenceWeightValue * 100) / 100);
        $("#adjustQtyValue").val(Math.round(adjustQty * 100) / 100);
        $("#adjustWeightValue").val(Math.round(adjustWeight * 100) / 100);

        js.table.destroy(table2);
        adjustStockList();
    }

    /** 打开盘亏模态框 */
    function lossUpdate(ajaItyId, d) {

        $("#ajaItyId").val(ajaItyId);
        $("#ajaCdlId").val($("#cdlId").val());
        $("#ajaId").val(d.ajaId);
        $("#ajaCpnId").val($("#cpnId").val());

        // 损益数量 损益重量 已经调整数量 已经调整重量 可用数量 可用重量
        var differenceQtyValue = $("#differenceQtyValue").val() == "" ? 0 : $("#differenceQtyValue").val();
        var differenceWeightValue = $("#differenceWeightValue").val() == "" ? 0 : $("#differenceWeightValue").val();
        var adjustQtyValue = $("#adjustQtyValue").val() == "" ? 0 : $("#adjustQtyValue").val();
        var adjustWeightValue = $("#adjustWeightValue").val() == "" ? 0 : $("#adjustWeightValue").val();

        // 还需调整数量和重量
        var needAdjustQty = Math.round((parseFloat(differenceQtyValue) - parseFloat(adjustQtyValue)) * 100) / 100;
        var needAdjustWeight = Math.round((parseFloat(differenceWeightValue) - parseFloat(adjustWeightValue)) * 100) / 100;
        // 可用数量和重量
        var enableNum = d.ajaItyNum;
        var enableWeight = d.ajaItyWeight;
        // 左侧本条物料已经填写的调整数量和调整重量
        var selfAdjustQty = d.ajaNum;
        var selfAdjustWeight = d.ajaWeight;

        if (selfAdjustWeight != null && selfAdjustWeight != "") {
            $("#adjustQty").val(selfAdjustQty);
            $("#adjustWeight").val(selfAdjustWeight);
        } else {
            // 如果还需调整数量>可用数量，模态框 调整数量和调整重量 显示 可用数量和可用重量
            if (needAdjustQty > enableNum) {
                $("#adjustQty").val(enableNum);
                $("#adjustWeight").val(enableWeight);
            } else if (needAdjustQty < enableNum) {
                // 如果还需调整数量<可用数量，模态框 调整数量和调整重量 显示 还需调整数量和还需调整重量
                $("#adjustQty").val(needAdjustQty);
                $("#adjustWeight").val(needAdjustWeight);
            } else if (needAdjustQty == enableNum) {
                // 如果相等 ，则显示可用数量和重量
                $("#adjustQty").val(enableNum);
                $("#adjustWeight").val(enableWeight);
            }

        }

        js.modal.open("modal-lossCreate");
        $("#adjustQty").focus();
    }

    /** 打开盘盈模态框 */
    function profitUpdate(ajaItyId, d) {

        $("#profitCdlId").val($("#cdlId").val());
        $("#profitAjaId").val(d.ajaId);
        $("#profitCpnId").val($("#cpnId").val());

        // 损益数量 损益重量
        var differenceQtyValue = $("#differenceQtyValue").val() == "" ? 0 : $("#differenceQtyValue").val();
        var differenceWeightValue = $("#differenceWeightValue").val() == "" ? 0 : $("#differenceWeightValue").val();

        // 左侧本条物料已经填写的调整数量和调整重量
        var selfAdjustQty = d.ajaNum;
        var selfAdjustWeight = d.ajaWeight;

        if (selfAdjustWeight != null && selfAdjustWeight != "") {
            $("#adjustProfitQty").val(selfAdjustQty);
            $("#adjustProfitWeight").val(selfAdjustWeight);
        } else {
            $("#adjustProfitQty").val(differenceQtyValue);
            $("#adjustProfitWeight").val(differenceWeightValue);
        }

        js.modal.open("modal-profitCreate");
        $("#adjustProfitQty").focus();
    }

    $(function () {

        /** 加载列表 */
        table1 = js.table.init({
            url: ctx + "material/checkPlan/adjustList",
            pageSize: 300,
            showExport: false,
            onClickRow: function (row) {
                table1ClickRow(row);
            },
            showExport: false,
            queryData: {cpnId: $("#cpnId").val()},
            columns: [
                {title: '物料描述', field: 'matNameDesc'},
                {title: '在库总数', field: 'cdlBeforeNum'},
                {title: '在库总量', field: 'cdlBeforeWeight'},
                {title: '盘点数量', field: 'cdlNum'},
                {title: '盘点重量', field: 'cdlWeight'},
                {
                    title: '数量损溢差', field: 'differenceNum',
                    formatter: function (value, row, index) {
                        return Math.round(value * 100) / 100;
                    }
                },
                {title: '重量损溢差', field: 'differenceWeight'},
                {title: '调整量', field: 'adjustWeight'}
            ]
        });

        /** 加载损溢明细调整 */
        adjustStockList();

        /** 添加盘亏记录 */
        $("#btn-lossCommit").click(function () {

            js.validSubmit({
                formId: "adjustLossForm",
                url: ctx + "material/checkPlan/adjustLoss",
                data: new FormData($("#adjustLossForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        var info = result.msg.split(",");
                        // 已经调整数量 已经调整重量赋值 ，目的：方便用户操作右侧数量 ，自动计算
                        $("#adjustQtyValue").val(info[0]);
                        $("#adjustWeightValue").val(info[1]);
                        js.modal.success("操作成功");
                        js.reset("adjustLossForm");
                        js.modal.hide("modal-lossCreate");
                        js.table.refresh(table1);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })
        /** 盘亏数量调整联动 */
        $("#adjustQty").change(function () {
            var spec = $("#matSpecName").val();
            var length = $("#matLength").val();
            var width = $("#matWidth").val();
            var height = $("#matHeight").val();
            var adjustQty = $("#adjustQty").val();
            var formulaType = $("#matFormulaType").val();

            var adjustWeight = returnMaterialWeight(spec, length, width, height, adjustQty, formulaType);
            $("#adjustWeight").val(adjustWeight);
        });

        /** 添加盘盈记录 */
        $("#btn-profitCommit").click(function () {
            js.validSubmit({
                formId: "adjustProfitForm",
                url: ctx + "material/checkPlan/adjustProfit",
                data: new FormData($("#adjustProfitForm")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success("操作成功");
                        js.reset("adjustProfitForm");
                        js.modal.hide("modal-profitCreate");
                        js.table.refresh(table1);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        })
        /** 盘盈数量调整联动 */
        $("#adjustProfitQty").change(function () {
            var spec = $("#matSpecName").val();
            var length = $("#matLength").val();
            var width = $("#matWidth").val();
            var height = $("#matHeight").val();
            var adjustQty = $("#adjustProfitQty").val();
            var formulaType = $("#matFormulaType").val();

            var adjustWeight = returnMaterialWeight(spec, length, width, height, adjustQty, formulaType);
            $("#adjustProfitWeight").val(adjustWeight);
        });

        /** 更新盘点损益调整状态 */
        $("#btn-updateAdjStatus").click(function () {
            js.modal.confirm("确定" + ($("#cpnAdjStatus").val() == 3 ? "撤销" : "提交") + "盘点损益调整吗？", function (result) {
                js.post({
                    url: ctx + "material/checkPlan/updateAdjStatus",
                    data: {
                        cpnId: $("#cpnId").val()
                    },
                    success: function (result) {
                        if (result.type === web_status.SUCCESS) {
                            js.modal.success(result.msg);
                            location.reload();
                        } else {
                            js.modal.warning(result.msg);
                        }
                        js.modal.closeLoading();
                    }
                })
            })
        });

    })
</script>
</body>
</html>